/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package basedatos;

import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author yojugracu
 */
public class DDL {
    
    public void crearTabla( Connection connection, String nombre_tabla, 
            ArrayList<String[]> campos )
    {
        String sql = "CREATE TABLE " + nombre_tabla + " { ";
        
        List<String[]> lista = campos;
        String columnas = "";
        for( String[] campo : lista ){
            for( String elemento : campo ){
                columnas += " " + elemento;
            }
            columnas +=",";
        }
        columnas = columnas.substring(0, columnas.length()-1);
        sql += columnas + " }" ;
    }
    public static boolean ejecutarScript(Connection connection,String script)
    {
        boolean ejecucion_exitosa=false;
        try{
            Statement stmt = connection.createStatement();
            stmt.execute(script);
            stmt.close();
            ejecucion_exitosa=true;
        }catch( Exception e ){  e.printStackTrace(); }
        return ejecucion_exitosa;
    }
    
    public final static void crearBaseDatos( Connection connection, String bd ){
        try{
            String script = "CREATE DATABASE " + bd 
                    + " WITH OWNER = postgres ENCODING = 'UTF8'"
                    + " TABLESPACE = pg_default"
                    /*+ " LC_COLLATE = 'Spanish_Guatemala.1252'"
                    + " LC_CTYPE = 'Spanish_Guatemala.1252'"*/
                    + " CONNECTION LIMIT = -1;";

            Statement stmt = connection.createStatement();
            stmt.execute( script );
            stmt.close();
        }catch( Exception e ){  e.printStackTrace(); }
    }
}
